package com.lonwin.inspectionmanagement.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hss01248.dialog.StyledDialog;
import com.hss01248.dialog.interfaces.MyDialogListener;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.lonwin.inspectionmanagement.R2;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.AppUtil;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.TimeUtils;
import com.lonwin.inspectionmanagement.di.component.DaggerEditPatrolRecordComponent;
import com.lonwin.inspectionmanagement.mvp.contract.EditPatrolRecordContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.XcjlbInfoBean;
import com.lonwin.inspectionmanagement.mvp.presenter.EditPatrolRecordPresenter;

import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.widget.dialog.LoadingDailog;


import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/24/2018 23:31
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class EditPatrolRecordActivity extends BaseActivity<EditPatrolRecordPresenter> implements EditPatrolRecordContract.View ,OnDateSetListener {

    @BindView(R2.id.back_icon)
    ImageView backIcon;
    @BindView(R2.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R2.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R2.id.back_right_icon)
    TextView backRightIcon;
    @BindView(R2.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R2.id.toolbar)
    Toolbar toolbar;
    @BindView(R2.id.patrolRecord_bt)
    EditText patrolRecordBt;
    @BindView(R2.id.patrolRecord_hhkq)
    EditText patrolRecordHhkq;
    @BindView(R2.id.patrolRecord_hz)
    EditText patrolRecordHz;
    @BindView(R2.id.patrolRecord_hhkqjbqk)
    EditText patrolRecordHhkqjbqk;
    @BindView(R2.id.patrolRecord_xcsj)
    TextView patrolRecordXcsj;
    @BindView(R2.id.patrolRecord_xcry)
    EditText patrolRecordXcry;
    @BindView(R2.id.patrolRecord_tq)
    EditText patrolRecordTq;
    @BindView(R2.id.patrolRecord_cydw)
    EditText patrolRecordCydw;
    @BindView(R2.id.patrolRecord_xcqy)
    EditText patrolRecordXcqy;
    @BindView(R2.id.patrolRecord_xcnr)
    EditText patrolRecordXcnr;
    @BindView(R2.id.patrolRecord_czwt)
    EditText patrolRecordCzwt;
    @BindView(R2.id.patrolRecord_hzclyj)
    EditText patrolRecordHzclyj;
    @BindView(R2.id.patrolRecord_qpimg)
    ImageView patrolRecordQpimg;
    @BindView(R2.id.patrolRecord_lay_qpimg)
    LinearLayout patrolRecordLayQpimg;

    @BindView(R2.id.patrolRecord_save)
    Button patrolRecordSave;
    @BindView(R2.id.patrolRecord_send)
    Button patrolRecordSend;
    private LoadingDailog loadingDailog;
    private XcjlbInfoBean.DataBean mdataBean;
    private TimePickerDialog mDialogAll;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerEditPatrolRecordComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_edit_patrol_record; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (getIntent().hasExtra("type"))
        {
            patrolRecordSend.setVisibility(View.GONE);
        }
        initTime();
        loadingDailog=new LoadingDailog.Builder(this).setMessage("处理中...").create();
        setlIstener();
        mPresenter.getXcjlbById(getIntent().getStringExtra(AppConstants.EXTRA_XCJLBID));
    }
    public void initTime(){
        mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(EditPatrolRecordActivity.this)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("请选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
//                         .setHourText("Hour")
//                         .setMinuteText("Minute")
                .setCyclic(false)
//            .setMinMillseconds(System.currentTimeMillis()-(100*365*24*60*60*1000))
//                         .setMaxMillseconds(System.currentTimeMillis()+10*1000 )
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.color_FF098DDB))
                .setType(Type.YEAR_MONTH_DAY)
//                         .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
//                         .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(14)
                .build();
    }
    public void setlIstener(){
        patrolRecordXcsj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialogAll.show(getSupportFragmentManager(),"巡河时间");
            }
        });
        patrolRecordSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String,String> maps=new HashMap<>();
                maps.put("cydw",patrolRecordCydw.getText().toString().trim());
                maps.put("czwt",patrolRecordCzwt.getText().toString().trim());
                maps.put("gpsid",mdataBean.getGpsid());
                maps.put("gpsnm",mdataBean.getGpsnm());
                maps.put("groupid",mdataBean.getGroupid());
                maps.put("groupnm",mdataBean.getGroupnm());
                maps.put("hzclyj",patrolRecordHzclyj.getText().toString().trim());
                maps.put("hzid",mdataBean.getHzid());
                maps.put("hznm",mdataBean.getHznm());
                maps.put("jbqk",patrolRecordHhkqjbqk.getText().toString().trim());
                maps.put("title",patrolRecordBt.getText().toString().trim());
                maps.put("tq",patrolRecordTq.getText().toString().trim());

                maps.put("xcjlbid",mdataBean.getXcjlbid());
                maps.put("xcnr",patrolRecordXcnr.getText().toString().trim());
                maps.put("xcqy",patrolRecordXcqy.getText().toString().trim());
                maps.put("xcrecordid",mdataBean.getXcrecordid());
                maps.put("xcry",patrolRecordXcry.getText().toString().trim());
                maps.put("xcsj",patrolRecordXcsj.getText().toString().trim());
                maps.put("role",mPresenter.getConfigBean().getCurrentRole().getRoleCode());
                 mPresenter.editXcjlb(maps);
            }
        });
        patrolRecordSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LookPatrolRecordActivity.startLookPatrolRecordActivity_new(EditPatrolRecordActivity.this,getIntent().getStringExtra(AppConstants.EXTRA_XCJLBID),LookPatrolRecordActivity.Type.BSHZQP,"巡查记录表预览");

//                StyledDialog.buildIosAlert("提示", "你确定要删除该条记录吗？", new MyDialogListener() {
//                    @Override
//                    public void onFirst() {
//                    }
//
//                    @Override
//                    public void onSecond() {
//                        StyledDialog.dismiss();
//                    }
//                }).show();
            }
        });
    }

    @Override
    public void getXcjlbByIdSuccess(XcjlbInfoBean.DataBean dataBean) {
        if (dataBean==null)return;
        mdataBean =dataBean;
        patrolRecordBt.setText(dataBean.getTitle()!=null?dataBean.getTitle():"");
        patrolRecordHhkq.setText(dataBean.getGroupnm()!=null?dataBean.getGroupnm():"");
        patrolRecordHz.setText(dataBean.getHznm()!=null?dataBean.getHznm():"");
        patrolRecordHhkqjbqk.setText(dataBean.getJbqk()!=null?dataBean.getJbqk():"");
        patrolRecordXcsj.setText(TimeUtils.getNowDayAndMonth(dataBean.getXcsj()));
        patrolRecordXcry.setText(dataBean.getXcry()!=null?dataBean.getXcry():"");
        patrolRecordTq.setText(dataBean.getTq()!=null?dataBean.getTq():"");
        patrolRecordCydw.setText(dataBean.getCydw()!=null?dataBean.getCydw():"");
        patrolRecordXcqy.setText(dataBean.getXcqy()!=null?dataBean.getXcqy():"");
        patrolRecordXcnr.setText(dataBean.getXcnr()!=null?dataBean.getXcnr():"");
        patrolRecordCzwt.setText(dataBean.getCzwt()!=null?dataBean.getCzwt():"");
        patrolRecordHzclyj.setText(dataBean.getHzclyj()!=null?dataBean.getHzclyj():"");
        if (dataBean.getHzclqm()!=null&&TextUtils.isEmpty(dataBean.getHzclqm()))
        {
            patrolRecordLayQpimg.setVisibility(View.VISIBLE);
            Glide.with(this).load(DataUtil.getFileUrl(mPresenter.getConfigBean())+"/"+dataBean.getHzclqm()).into(patrolRecordQpimg);

        }else {
            patrolRecordLayQpimg.setVisibility(View.GONE);
        }
    }
    @Override
    public void showLoading() {
        loadingDailog.show();
    }

    @Override
    public void hideLoading() {
        loadingDailog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getMyActivity() {
        return this;
    }


    @Override
    public void editXcjlbSuccess(XcjlbInfoBean.DataBean dataBean) {
           killMyself();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1001&&resultCode==RESULT_OK)
        {
            killMyself();
        }
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        patrolRecordXcsj.setText(TimeUtils.longToStrTime(millseconds));
    }
}
